<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_admin extends base_control{

	//hook ctr_admin_before_init.php

	public function __construct() {
		parent::__construct();
		
		$this->check_user_login_msg();
		$this->check_gid100();
		
		//hook ctr_admin_init_init.php
		
		$this->tpl->assign('_site_sub_keywords', '管理后台');
		$this->tpl->assign('_site_sub_description', '管理后台');
		$this->tpl->assign('_site_sub_title', '管理后台');
		//hook ctr_admin_end_init.php
	}
	
	public function do_index() {
		//hook ctr_admin_before_index.php
		
		$this->tpl->display('adm_index.tpl');
	}
	
	public function do_siteset() {
		$arr = array();
		$edit_msg = '';
		//hook ctr_admin_before_site_set.php
		
		$sub = core::gpc('sub','P');
		if(!empty($sub)) {
			$arr['_site_title'] = core::gpc('title','P');
			$arr['_site_keywords'] = core::gpc('keywords','P');
			$arr['_site_description'] = core::gpc('description','P');
			$arr['_site_generator'] = core::gpc('generator','P');
			$arr['_site_author'] = core::gpc('author','P');
			$arr['_site_copyright'] = core::gpc('copyright','P');
			
			//hook ctr_admin_before_site_conf_set.php
			if($this->conf->set_site_conf_arr($arr)) {
				$this->message('设置成功','index.php?admin-siteset.htm');
			}else{
				$edit_msg = '设置失败';
			}
		}
		//hook ctr_admin_end_site_set.php
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_siteset.tpl');
	}
	
	public function do_bbsset() {
		$arr = $this->conf->get_misc_conf_arr();
		$arr2 = $this->conf->get_file_conf_arr();
		$edit_msg = '';
		$_file_ext_str = '';
		$_imgs_ext_str = '';
		
		$sub = core::gpc('sub','P');
		//hook ctr_admin_before_bbs_set.php
		if(!empty($sub)) {
			$arr2['_file_allow_size'] = core::gpc('allow_size','P');
			
			$arr2['_file_file_ext'] = array();
			$tmp1 = explode(';', core::gpc('file_ext','P'));
			foreach($tmp1 as $k=>$v) {
				if(!empty($v)) $arr2['_file_file_ext'][] = $v;
			}
			$arr2['_file_imgs_ext'] = array();
			$tmp1 = explode(';', core::gpc('imgs_ext','P'));
			foreach($tmp1 as $k=>$v) {
				if(!empty($v)) $arr2['_file_imgs_ext'][] = $v;
			}
			
			$arr['_conf_reglock'] = intval(core::gpc('reglock','P'));
			$arr['_conf_viewlock'] = intval(core::gpc('viewlock','P'));
			$arr['_conf_bbs_pagesize'] = intval(core::gpc('pagesize','P'));
			$arr['_conf_bbs_commentsize'] = intval(core::gpc('commentsize','P'));
			
			//hook ctr_admin_in_bbs_set.php
			if($this->conf->set_misc_conf_arr($arr)
				&& $this->conf->set_file_conf_arr($arr2)) {
				$this->message('设置成功','index.php?admin-bbsset.htm');
			}else{
				$edit_msg = '设置失败';
			}
		}
		foreach($arr2['_file_file_ext'] as $k=>$v)
			$_file_ext_str .= $v.';';
		foreach($arr2['_file_imgs_ext'] as $k=>$v)
			$_imgs_ext_str .= $v.';';
		
		//hook ctr_admin_end_bbs_set.php
		$this->tpl->assign('_file_allow_size', $arr2['_file_allow_size']);
		$this->tpl->assign('_file_file_ext', $_file_ext_str);
		$this->tpl->assign('_file_imgs_ext', $_imgs_ext_str);
		$this->tpl->assign('_conf_bbs_commentsize', $arr['_conf_bbs_commentsize']);
		$this->tpl->assign('_conf_reglock', $arr['_conf_reglock']);
		$this->tpl->assign('_conf_bbs_pagesize', $arr['_conf_bbs_pagesize']);
		$this->tpl->assign('_conf_viewlock', $arr['_conf_viewlock']);
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_bbsset.tpl');
	}
	
	public function do_regset() {
		$reginfo = $this->conf->get_conf_arr(5);
		$edit_msg = '';
		
		$sub = core::gpc('sub','P');
		//hook ctr_admin_reg_set_start.php
		if(!empty($sub)) {
			$reginfo['regnotice'] = core::gpc('reginfo','P');
			//hook ctr_admin_reg_set_before.php
			if($this->conf->set_conf_arr($reginfo, 5)) {
				$this->message('设置成功','index.php?admin-regset.htm');
			}else{
				$edit_msg = '设置失败';
			}
		}
		//hook ctr_admin_reg_set_end.php
		$this->tpl->assign('reginfo', $reginfo['regnotice']);
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_regset.tpl');
	}
	
	public function do_forumset() {
		$sudo = core::gpc('sudo','G');
		$id = intval(core::gpc('id','G'));
		$display = intval(core::gpc('display','P'));
		$name = core::gpc('name','P');
		$master = core::gpc('master','P');
		$edit_msg = '';
		//hook ctr_admin_forum_set_start.php
		switch($sudo) {
		case 'edit':
			//hook ctr_admin_forum_set_in_edit.php
			if($id >0) {
				$this_f = $this->forum->forum_get($id);
				if(isset($this_f['master']) ){
					$master_old = explode(',',$this_f['master']);
					$master_old = $master_old == false ? array():$master_old;
					$master_old = array_unique(array_filter($master_old));
					$master_new = explode(',',$master);
					$master_new = $master_new == false ? array():$master_new;
					$master_new = array_unique(array_filter($master_new));
					foreach($master_old as $k=>$v) {
						if($v <= 2) continue;
						if(!in_array($v,$master_new)){
							$this->user->edit_user($v,0,1);
						}
					}
					foreach($master_new as $k=>$v) {
						if($v <= 2) continue;
						if(!in_array($v,$master_old)){
							$this->user->edit_user($v,0,4);
						}
					}
				}
			}
			if($id >0 && !empty($name) && $this->forum->forum_edit($id,$name,$master,$display))
				$this->message('修改成功','index.php?admin-forumset.htm');
			
			$edit_msg = $id >0 ? '修改失败':'回收站不能被设置';
			break;
		case 'add':
			//hook ctr_admin_forum_set_in_add.php
			if(!empty($name) && $this->forum->forum_add($name,$display))
				$this->message('添加成功','index.php?admin-forumset.htm');
			
			$edit_msg = '添加失败';
			break;
		//hook ctr_admin_forum_set_in_switch.php
		default:
			break;
		}
		$arr = $this->forum->forum_list();
		
		//hook ctr_admin_forum_set_end.php
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->assign('forumlist', $arr);
		$this->tpl->display('adm_forum.tpl');
	}
	
	public function do_utitleset() {
		$utitle = $edit_msg = '';
		$utitle_arr = array();
		$utitle_post = core::gpc('utitle','P');
		//hook ctr_admin_utitle_set_start.php
		if(!empty($utitle_post)) {
			$tmp = explode(';' ,$utitle_post);
			foreach($tmp as $k=>$v) {
				if(!empty($v))
					$utitle_arr[] = $v;
			}
			//hook ctr_admin_utitle_set_before.php
			if($this->conf->set_conf_arr($utitle_arr, 6)) {
				$edit_msg = '保存成功';
			}
		}
		$arr = $this->conf->get_conf_arr(6);
		$utitle_arr = array();
		foreach($arr as $k=>$v) {
			$utitle .= "$v;";
			$this->miscfunc->get_core_level($k);
			$utitle_arr[$k] = $v;
		}
		//hook ctr_admin_utitle_set_end.php
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->assign('utitle_arr', $utitle_arr);
		$this->tpl->assign('utitle', $utitle);
		$this->tpl->display('adm_utitle.tpl');
	}
	
	public function do_userlist() {
		$edit_msg = '';
		
		$uid = abs(intval(core::gpc('uid', 'G')));
		$g = core::gpc('g', 'G');
		//hook ctr_admin_user_list_start.php
		if($g == 'f' && $uid > 2) {
			//hook ctr_admin_user_lock_before.php
			$edit_msg = '封锁用户失败';
			if($this->user->edit_user($uid,0,200)) {
				
				$edit_msg = '成功封锁用户';
			}
		}elseif($g == 'j' && $uid > 2){
			//hook ctr_admin_user_unlock_before.php
			$edit_msg = '解封用户失败';
			if($this->user->edit_user($uid,0,1)) {
				
				$edit_msg = '成功解封用户';
			}
		}elseif($g == 'g' && $uid > 2) {
			//hook ctr_admin_user_edit_pass_before.php
			$u = $this->user->get_user_by_uid($uid, false);
			$edit_msg = '改用户密码失败';
			$pass = rand(100000,999999);
			if($this->user->edit_user($uid,0,-1,'','',$pass,$u['name'])){
			
				$edit_msg = '改用户密码成功，随机密码：'.$pass;
			}
		}
		//hook ctr_admin_user_list_done_end.php
	
		$pagesize = $limit = 10;
		$page	= html::page('page');
		$count = $this->db->count('user-uid');
		$pages = html::pages('index.php?admin-userlist.htm', $count, $page, $pagesize);
		$start = ($page-1)*$pagesize;
		$users = $this->user->user_list(array(), array('uid'=>1), $start, $limit);
	
		//hook ctr_admin_user_list_end.php
		$this->tpl->assign('users', $users);
		$this->tpl->assign('pages', $pages);
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_ulist.tpl');
	
	}
	
	public function do_tpl() {
		$f = core::gpc('f', 'G');
		$content = $file = $edit_msg = '';
		$files = array();
		
		//hook ctr_admin_tpl_start.php
		if(!empty($f)) {
			$file = $f = base64_decode($f);
			$f = CONFIG_TPL_PATH. $f;
			if(!file_exists($f))
				$f = NULL;
		}
		if(!empty($f)) {
			$content = core::gpc('content', 'P');
			$content = htmlspecialchars_decode($content);
			//hook ctr_admin_tpl_edit_before.php
			if(!empty($content)){
				file_put_contents($f,$content,LOCK_EX);
				$edit_msg = '修改成功';
			}
		
			$content = file_get_contents($f);
			$content = core::htmlspecialchars($content);
			//hook ctr_admin_tpl_edit_end.php
		}else{
			$files = misc::scandir(CONFIG_TPL_PATH);
			//hook ctr_admin_tpl_list_end.php
		}
		//hook ctr_admin_tpl_end.php
		$this->tpl->assign('files',$files);
		$this->tpl->assign('file',$file);
		$this->tpl->assign('content', $content);
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_tpl.tpl');
	}
	
	public function do_clear() {
		$edit_msg = '<br/>';
		//hook ctr_admin_clear_start.php
		if(misc::rmdir(CONFIG_TMP_PATH, array('index.html'), 1)) {
			$edit_msg .= '框架代码缓存清理成功';
		}else{
			$edit_msg .= '框架代码缓存清理失败';
		}
		
		//hook ctr_admin_clear_mainview_start.php
		$edit_msg .= '<br/>';
		if(misc::rmdir(CONFIG_TPL_CATH, array('index.html'), 1)) {
			$edit_msg .= '模板缓存清理成功';
		}else{
			$edit_msg .= '模板缓存清理失败';
		}
		
		$this->this_clear = true;
		//hook ctr_admin_clear_end.php
	
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_clear.tpl');
	}
	
	public function do_tfile() {
		$edit_msg = '';
		
		$md5 = core::gpc('md5', 'G');
		//hook ctr_admin_tfile_start.php
		if(!empty($md5) && $this->attach->attach_del($md5))
			$edit_msg = '删除成功';

		//hook ctr_admin_tfile_page_start.php
		$pagesize = $limit = 10;
		$page	= html::page('page');
		$count = $this->attach->attach_count_untarget();
		$pages = html::pages('index.php?admin-tfile.htm', $count, $page, $pagesize);
		$start = ($page-1)*$pagesize;
		$attaches = $this->attach->attach_list_untarget($start, $limit);
		
		//hook ctr_admin_tfile_page_end.php
		foreach($attaches as &$u) {
			$user = $this->user->get_user_by_uid($u['uid'], false);
			$u['size'] = misc::humansize(abs(filesize(IN_APP.$u['path'])));
			$u['username'] = $user['name'];
		}
		//hook ctr_admin_tfile_end.php
		$this->tpl->assign('attaches', $attaches);
		$this->tpl->assign('pages', $pages);
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_tfile.tpl');
	
	}
	
	public function do_tj() {
		if(!file_exists(JY_TJAPI_FILE)) {
			file_put_contents(JY_TJAPI_FILE, "<?php //jy_tj ?>", LOCK_EX);
		}
		$tj = '';
		
		//hook ctr_admin_tj_start.php
		$edit_msg = '';
		
		$sub = core::gpc('sub','P');
		if(!empty($sub)) {
			//hook ctr_admin_tj_edit_start.php
			$tj = core::gpc('content','P');
			$tj = htmlspecialchars_decode($tj);
			file_put_contents(JY_TJAPI_FILE, $tj, LOCK_EX);
			$this->message('设置成功','index.php?admin-tj.htm');
			
			//hook ctr_admin_tj_edit_end.php
		}
		$tj = file_get_contents(JY_TJAPI_FILE);
		$tj = core::htmlspecialchars($tj);
		
		//hook ctr_admin_tj_end.php
		$this->tpl->assign('content', $tj);
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_tj.tpl');
		
	}
	
	public function do_logs() {
		$f = core::gpc('f', 'G');
		$c = core::gpc('c', 'G');
		$content = $file = $edit_msg = '';
		$files = array();
		
		//hook ctr_admin_logs_start.php
		if(!empty($c)){
			//hook ctr_admin_logs_rm_start.php
			if(misc::rmdir(CONFIG_LOG_PATH, array('index.html'), 1)) {
				$edit_msg .= '日志清理成功';
			}else{
				$edit_msg .= '日志清理失败';
			}
		}
		
		if(!empty($f)) {
			$file = $f = base64_decode($f);
			$f = CONFIG_LOG_PATH . $f;
			if(!file_exists($f))
				$f = NULL;
		}
		if(!empty($f)) {
			$content = file_get_contents($f);
			$content = core::htmlspecialchars($content);
			//hook ctr_admin_logs_get_end.php
		}else{
			$files = misc::scandir(CONFIG_LOG_PATH);
			//hook ctr_admin_logs_list_end.php
		}
		//hook ctr_admin_logs_end.php
		$this->tpl->assign('files',$files);
		$this->tpl->assign('file',$file);
		$this->tpl->assign('content', $content);
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_log.tpl');
	}
	
	public function do_plgs() {
		$edit_msg = '';
	
		$id = core::gpc('id', 'G');
		$d = core::gpc('d', 'G');
		//hook ctr_admin_plgs_start.php
		if($d == 'i' && !empty($id)) {
			//hook ctr_admin_plgs_install_before.php
			if($this->plugin->plugin_install($id)){
				core::$installed_plugin_conf['baseinfo'] = array();
				$this->message('安装插件成功', 'index.php?admin-plgs.htm');
			}
			$edit_msg = '安装插件失败';
			//hook ctr_admin_plgs_install_end.php
		}elseif($d == 'u' && !empty($id)){
			//hook ctr_admin_plgs_uninstall_before.php
			if($this->plugin->plugin_uninstall($id)){
				core::$installed_plugin_conf['baseinfo'] = array();
				$this->message('卸载插件成功', 'index.php?admin-plgs.htm');
			}
			$edit_msg = '卸载插件失败';
			//hook ctr_admin_plgs_uninstall_end.php
		}
		
		$ins_plgs = core::$installed_plugin_conf['baseinfo'];
		$plgs = core::get_paths(CONFIG_PLG_PATH);
		$plg_list = array();
		//hook ctr_admin_plgs_conf_after.php
		
		foreach($plgs as $k=>$plgid) {
			$plg_conf_file = CONFIG_PLG_PATH. $plgid.'/conf.php';
			if(!file_exists($plg_conf_file)) continue;
			$plg_conf = include($plg_conf_file);
			$rand = isset($plg_conf['baseinfo']['rand'])?$plg_conf['baseinfo']['rand']:0;
			if(empty($rand)) continue;
			$plg_list[$rand]['install'] = 0;
			if(array_key_exists($rand, $ins_plgs)){
				$plg_list[$rand]['install'] = 1;
			}
			$plg_list[$rand]['baseinfo'] = $plg_conf['baseinfo'];
			$plg_list[$rand]['moreinfo'] = $plg_conf['moreinfo'];
			//hook ctr_admin_plgs_list_end_foreach.php
		}
		
		//hook ctr_admin_plgs_end.php
		$this->tpl->assign('plg_list', $plg_list);
		$this->tpl->assign('edit_msg', $edit_msg);
		$this->tpl->display('adm_plugin.tpl');
	}
	
	//==================>private
	private function check_gid100() {
		$gid = $this->user->user_groupid();
		//hook ctr_admin_in_check_gid100.php
		if($gid != 100)
			$this->message('权限不足','index.php');
	}

	//hook ctr_admin_end.php
}
/*{TranslateTagEnd}*/

?>